package com.example.qq.adpter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.example.qq.R;
import com.example.qq.bean.Msg;
import com.example.qq.databinding.ItemMsgBinding;

import java.util.List;

public class MsgAdatper extends RecyclerView.Adapter<MsgAdatper.ViewHolder> {
    private List<Msg> mMsgList;
    private Context mContext;

    public MsgAdatper(Context context, List<Msg> msgList) {
        mContext = context;
        mMsgList = msgList;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ItemMsgBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item_msg, parent, false);
        return new ViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        Msg msg = mMsgList.get(position);
        holder.binding.setMsg(msg);
        // holder.binding.executePendingBindings();//如果下拉刷新数据闪烁，使用该行代码

      /*  //设置binding中的点击监听
        holder.binding.setClick(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //获取到对应位置的user对象
                int position = holder.getAdapterPosition();
                User user = mUserList.get(position);
                switch (view.getId()) {
                    case R.id.item_name:
                        Toast.makeText(mContext, "点击了" + user.getUsername(), Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.item_iv:
                        Toast.makeText(mContext, "点击了图片" + user.getUserface(), Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        });*/
    }

    @Override
    public int getItemCount() {
        return mMsgList.size();
    }

    //只需要一个ViewDataBinding的实例对象，因为所有控件都绑定在了Binding对象中
    //ItemUserBinding类是因为编写了对应的item_user.xml生成的
    class ViewHolder extends RecyclerView.ViewHolder {
        ItemMsgBinding binding;

        public ViewHolder(ItemMsgBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }
}